package my.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSONObject;

import af.spring.AfRestData;
import my.db.Assess;
import my.db.AssessPublishtable;
import my.tool.MybatisApi;

@Controller
public class AssessJoinController {
	
	private int AS_id;
	
	@GetMapping("assessjoin")
	public String AssessJoin(Model model,HttpServletRequest request) {
		
		model.addAttribute("CourseTeacherList", test(request));
		
		model.addAttribute("TeacheredList", test3(request));
		model.addAttribute("datas", IndexController.SimpleJS(request));
		
		if(test2(request).size() != 0) {
			model.addAttribute("QuestionList", test2(request));
		}else {
			return "NotFound";
		}
		
		
		return "Assess_Join";
	}
	
	@PostMapping("AJ_saveAssess.do")
	public Object saveAssess(@RequestBody JSONObject jreq) {
		
		Assess as = new Assess();
		as.setAppraisee(jreq.getInteger("Appraisee"));
		as.setAppraiser(jreq.getInteger("Appraiser"));
		as.setRole(jreq.getString("Role"));
		as.setPoint(jreq.getDouble("Point"));
		as.setAS_id(jreq.getInteger("AS_id"));
		MybatisApi.insert("zhy.sesAssessJoin.saveAssess", as);
		
		return new AfRestData("");
	}
	
	
	
	//获取和这个老师所在班级共事的其他老师信息
	public List<Map<String, Object>> test(HttpServletRequest request) {
		JSONObject s = (JSONObject) IndexController.SimpleJS(request);
		JSONObject ss = s.getJSONObject("jobj");
		List<Map<String, Object>> M = new ArrayList<Map<String,Object>>();
		if(String.valueOf(ss.getInteger("userId")).length() > 6) {
			M = MybatisApi.selectList("zhy.sesAssessJoin.getClazzTeacher", ss.getInteger("clazzId"));
			return M;
		}else {
			if(ss.getBoolean("isDean")) {
				
				M =  MybatisApi.selectList("zhy.sesAssessJoin.getAllTeacher",0 );
				return M;
			}else {
				//获取这个老师所在的所有班级
				List<Integer> class_idList = MybatisApi.selectList("zhy.sesAssessJoin.getClass_id",ss.getInteger("userId") );
				
				List<List<Map<String, Object>>> k = new ArrayList();
				for (int i=0;i<class_idList.size();i++) {
					k.add(MybatisApi.selectList("zhy.sesAssessJoin.getCourseTeacher", class_idList.get(i)));
				}
				
				
				for(int i=0;i<k.size();i++) {
					for(int j=0;j<k.get(i).size();j++) {
						M.add(k.get(i).get(j));
					}
				}
				
				//剔除重复等等老师信息
				 HashSet h = new HashSet(M);   
				    M.clear();   
				    M.addAll(h);   
				    return M;   
			}
		}
		
	}
	
	
	//获取对应角色的问卷问题
	public List<Object> test2(HttpServletRequest request) {
		JSONObject s = (JSONObject) IndexController.SimpleJS(request);
		JSONObject ss = s.getJSONObject("jobj");
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> objectList = new ArrayList<Object>();
		
		if( String.valueOf(ss.getInteger("userId")).length()>6) {
			map.put("role", "学生");
		}else {
			if(ss.getBoolean("isManager")) {	
				map.put("role", "院系主任");
			}else {
				map.put("role", "教师");
			}
		}
		if(MybatisApi.select("zhy.sesAssessJoin.getRole_id", map) != null) {
			int user_id = (int) MybatisApi.select("zhy.sesAssessJoin.getRole_id", map);
			AS_id = user_id;

			objectList = MybatisApi.selectList("zhy.sesAssessJoin.getQuestion", user_id);
		}
		return objectList;
	}
	
	//已评价过的教师
	public List<Integer> test3(HttpServletRequest request) {
		JSONObject s = (JSONObject) IndexController.SimpleJS(request);
		JSONObject ss = s.getJSONObject("jobj");
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId",  ss.getInteger("userId"));
		map.put("AS_id",AS_id);
		List<Integer> TeacheredList = MybatisApi.selectList("zhy.sesAssessJoin.getTeachered_id", map);
		
		return TeacheredList;
	}
}
